package com.guaju.yueshangLife.widget;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.guaju.yueshangLife.R;

public class GoodsTopBar extends com.zhy.autolayout.AutoRelativeLayout {


    private View view;

    public interface OnMyTabClickListener {
        void onTabClick(int position);
    }


    private OnMyTabClickListener onTabClickListener;
    private LinearLayout ll_title;
    private View left_iv;
    private View right_iv;

    public void setOnTabClickListener(OnMyTabClickListener onTabClickListener) {
        this.onTabClickListener = onTabClickListener;
    }

    public GoodsTopBar(Context context) {
        super(context);
        init();
    }


    public GoodsTopBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public GoodsTopBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    private void init() {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        view = inflater.inflate(R.layout.top_bar, this);
//        view.setBackgroundColor(getResources().getColor(R.color.transparent));
        view.setBackgroundColor(getResources().getColor(R.color.white));
        //top bar 文字部分
        ll_title = (LinearLayout) view.findViewById(R.id.ll_title);
        //左 图片
        left_iv = view.findViewById(R.id.left_iv);
        //右 图片
        right_iv = view.findViewById(R.id.right_iv);
        //top bar 文字数量
        int childCount = ll_title.getChildCount();
        if (childCount > 0) {
            for (int i = 0; i < childCount; i++) {
                ViewGroup tab = (ViewGroup) ll_title.getChildAt(i);
                tab.setOnClickListener(new OnTabClickListener(i));
            }
        }
        left_iv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (getContext() instanceof Activity){
                    Activity activity=(Activity)getContext();
                    activity.finish();
                }
            }
        });
    }

    /**
     * tab 的点击事件，更改颜色，并且允许用户自定义
     */
    private class OnTabClickListener implements OnClickListener {
        private int position = 0;

        public OnTabClickListener(int position) {
            this.position = position;
        }

        @Override
        public void onClick(View view) {
            if (view instanceof ViewGroup) {
                ViewGroup vg = (ViewGroup) view;
                setCurrentTab(vg);
                if (onTabClickListener != null) {
                    onTabClickListener.onTabClick(position);
                }
            }
        }
    }

    /**
     * 设置为当前tab
     * @param tab tab viewgroup
     */
    private void setCurrentTab(ViewGroup tab) {
        TextView tab_title = (TextView) tab.getChildAt(0);
        View tab_line = tab.getChildAt(1);
        clearAllTabs();
        tab_title.setTextColor(getResources().getColor(R.color.checked_text_color));
        tab_line.setVisibility(View.VISIBLE);
    }

    /**
     * 设置为当前tab
     * @param tab 索引
     */
    public void setCurrentTab(int tab) {
        clearAllTabs();
        if (tab < ll_title.getChildCount()) {
            ViewGroup child = (ViewGroup) ll_title.getChildAt(tab);
            setCurrentTab(child);
        }

    }

    /**
     * 清空所有选中状态
     */
    private void clearAllTabs() {
        int childCount = ll_title.getChildCount();
        if (childCount > 0) {
            for (int i = 0; i < childCount; i++) {
                ViewGroup tab = (ViewGroup) ll_title.getChildAt(i);
                TextView tab_title = (TextView) tab.getChildAt(0);
                View tab_line = tab.getChildAt(1);
                tab_line.setVisibility(View.INVISIBLE);
                tab_title.setTextColor(getResources().getColor(R.color.text_color_222));
            }
        }
    }

    /**
     * 将tab条隐藏，仅显示 left_iv 和 right_iv
     */
    public void setTabInvisible(){
        ll_title.setVisibility(View.INVISIBLE);
        setBackgroundColor(getResources().getColor(R.color.transparent));

    }

}
